package com.example.androidframework.ui.login

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import androidx.core.app.ActivityCompat
import androidx.lifecycle.viewModelScope
import com.example.androidframework.ui.main.MainActivity
import com.martin.lib_base.base.BaseViewModel
import com.martin.lib_base.constant.ConstantKey
import com.martin.lib_base.ktx.finish
import com.martin.lib_base.ktx.handle
import com.martin.lib_base.ktx.normalToast
import com.martin.lib_base.ktx.verifyNullOrEmpty
import com.martin.lib_base.ktx_live.buildRequest
import com.martin.lib_base.ktx_live.launchLoadingCollect
import com.martin.lib_base.net.NetApply
import com.martin.lib_base.utils.UserUtil
import com.tencent.mmkv.MMKV
import dev.DevUtils
import dev.utils.app.DeviceUtils
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch

class LoginViewModel : BaseViewModel() {
    var account = MutableStateFlow("").apply {
        // 获取保存的账号
        MMKV.defaultMMKV().decodeString(ConstantKey.ACCOUNT)?.let {
            viewModelScope.launch {
                // 将新值发送到数据流中
                emit(it)
            }
        }
    }
    var password = MutableStateFlow("")
    var agree = MutableStateFlow(false)

    fun goBack() {
        // 运用BaseViewModel的finish方法
        finish()
    }

    fun goBack(context: Context) {
        context.finish()
    }

    fun login() {
        if (account.value.verifyNullOrEmpty("请输入您的账号") || password.value.verifyNullOrEmpty("请输入登录密码")) {
            return
        }
        if (!agree.value) {
            normalToast("请先阅读并同意《用户协议》及《隐私政策》")
            return
        }
        loginRequest()
    }

    private fun loginRequest() {

        launchLoadingCollect({

            val uuid = if (ActivityCompat.checkSelfPermission(
                    context,
                    Manifest.permission.READ_PHONE_STATE
                ) != PackageManager.PERMISSION_GRANTED
            ) {
                java.util.UUID.randomUUID().toString()
            } else {
                DeviceUtils.getUUID()
            }


            NetApply.api.login(
                buildRequest(
                    "Login.UserLogin",
                    mapOf(
                        "country_code" to "+86",
                        "source" to "Android",
                        "user_login" to account.value,
                        "user_pass" to password.value,
                        "device_code" to uuid,
                    )
                )
            )
        }) {
            onSuccess = {
//                it.info?.let {
//                    val activity = DevUtils.getTopActivity()
//                    MainActivity.start(activity)
//                    context.finish()
//                }

                it.info.handle(
                    { userBean ->
                        UserUtil.handleLogin(userBean)
                        val activity = DevUtils.getTopActivity()
                        MainActivity.start(activity)
                        context.finish()
                    },{
                        normalToast("用户信息获取失败")
                    }
                )
            }
        }
    }
}